/* File:        	Evaluate.java
* Package:			org.esi.authz.performance
* Created:      	29-abr-2011
* Last Changed: 	0:08:07
* Author:       	Borja Urkizu borja.urquizu@tecnalia.com
*
*   
* History:
*  Revision 1.0.0.0  
* 
* Tecnalia Research and Innovation
*/
package com.tecnalia.authz.performance;

import org.jboss.security.xacml.interfaces.RequestContext;
import org.jboss.security.xacml.interfaces.ResponseContext;

import com.tecnalia.authz.common.XACMLConstants;
import com.tecnalia.authz.common.log.PDPLog;
import com.tecnalia.authz.common.request.Request;
import com.tecnalia.authz.pdp.config.PDPConfiguration;
import com.tecnalia.authz.pdp.core.PDPEngine;


// TODO: Auto-generated Javadoc
/**
 * The Class Evaluate.
 */
public class Evaluate {
	   
	   	/** The logger. */
	   	static PDPLog logger = new PDPLog(Evaluate.class);
		   
	   	/** The total request. */
	   	static int totalRequest = 0;
		   
	   	/** The mode. */
	   	static int mode = 0;
	       
	    /** The sample. */
	    static PDPEngine sample = null;

	    /** The user. */
    	public String user = "user3";
	    
    	/** The action. */
    	public String action = "read";
	    
       /**
        * Instantiates a new evaluate.
        */
       public Evaluate(){
           PDPConfiguration authzCfg = new PDPConfiguration();
			//Create a PDP according to the configuration
			try {
				sample = new PDPEngine();
				//Start the daemon
				//sample.start();
			} catch (Exception e) {
	        	logger.info("Error occurs when initialize system, will exit now.");
	            e.printStackTrace(System.err);

	            if (sample != null) {
	                sample.cleanup();
	            }
			}			
    	   
       }
       
		/**
		 * Request flexible.
		 *
		 * @param serviceList the service list
		 * @param performance the performance
		 * @return the response context
		 */
		public ResponseContext RequestFlexible(String [] serviceList, PDP performance) {
			
			// Evaluate the list of request against PDP which may hold 100, 1000 or 10000 policies. Let PDP retrieve
			// the matchable policies and evaluate them
			long time = System.currentTimeMillis();
			
			//Create a request that evaluates to Permit - 2.1
			Request req = new Request();
			RequestContext request = req.CreateRequestAttrList(user, 
															serviceList, 
															action, 
															"non-functional-role");			
			
			
			ResponseContext responseContext = null;
			
			try {
				responseContext = sample.evaluate(request, XACMLConstants.XACML_QUERY_Flexible);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			//ResponseContext responseContext = sample.evaluateRequestAgainstPDP(request);
			performance.setMiliseconds(System.currentTimeMillis() - time);
			
			//Log the response:
			// - 0 : Permit
			// - 1 : Deny
			//logger.debug("XACMLResponse : " + responseContext.getDecision());
					
	        return responseContext;
		}

		/**
		 * Request strict.
		 *
		 * @param serviceList the service list
		 * @param performance the performance
		 * @return the response context
		 */
		public ResponseContext RequestStrict(String [] serviceList, PDP performance) {
			
			// Evaluate the list of request against PDP which may hold 100, 1000 or 10000 policies. Let PDP retrieve
			// the matchable policies and evaluate them
			long time = System.currentTimeMillis();
			
			//Create a request that evaluates to Permit - 2.1
			Request req = new Request();
			RequestContext request = req.CreateRequestAttrList(user, 
															serviceList, 
															action, 
															"non-functional-role");
			
			ResponseContext responseContext = null;
			
			try {
				responseContext = sample.evaluate(request, XACMLConstants.XACML_QUERY_Strict);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			//ResponseContext responseContext = sample.evaluateRequestAgainstPDP(request);
			performance.setMiliseconds(System.currentTimeMillis() - time);
			
			//Log the response:
			// - 0 : Permit
			// - 1 : Deny
			//logger.debug("XACMLResponse : " + responseContext.getDecision());
					
	        return responseContext;
		}

		/**
		 * Request simple.
		 *
		 * @param serviceList the service list
		 * @param performance the performance
		 * @return the response context
		 */
		public ResponseContext RequestSimple(String [] serviceList, PDP performance) {
			
			// Evaluate the list of request against PDP which may hold 100, 1000 or 10000 policies. Let PDP retrieve
			// the matchable policies and evaluate them
			long time = System.currentTimeMillis();
			
			//Create a request that evaluates to Permit - 2.1
			Request req = new Request();
			RequestContext request = req.CreateRequest(user, 
													serviceList, 
													action, 
													"non-functional-role");

			ResponseContext responseContext = null;
			try {
				//ResponseContext[] responseContext = sample.evaluateCustomRequestAgainstPDP(request);
				responseContext = sample.evaluate(request, XACMLConstants.XACML_QUERY_Simple);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			performance.setMiliseconds(System.currentTimeMillis() - time);
			
			//Log the response:
			// - 0 : Permit
			// - 1 : Deny
			//logger.debug("XACMLResponse : " + responseContext.getDecision());
					
	        return responseContext;
		}
	
	
	
}
